package org.jeecg.common.util.result;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @author guoyh
 * @ClassName Result
 * @Description
 * @since 2020/8/11 22:31
 **/
@Accessors(chain = true)
@Data
public class Result<T> implements Serializable {

    /**
     * 错误码：参数非法
     */
    public static final int ILLEGAL_ARGUMENT_CODE = -100;
    /**
     * 错误信息：参数非法
     */
    public static final String ILLEGAL_ARGUMENT_MESSAGE = "参数非法";
    /**
     * 错误码：时间过期
     */
    public static final int TIME_OUT_CODE = -110;
    /**
     * 错误信息：时间过期
     */
    public static final String TIME_OUT_MESSAGE = "安全认证失败，时间过期";
    /**
     * 错误码：安全认证失败
     */
    public static final int SECURE_FAIL_CODE = -120;
    /**
     * 错误信息：安全认证失败
     */
    public static final String SECURE_FAIL_MESSAGE = "安全认证失败，请重新登陆";
    /**
     * 成功码.
     */
    public static final int SUCCESS_CODE = 1;
    /**
     * 成功信息.
     */
    public static final String SUCCESS_MESSAGE = "OK";
    /**
     * 错误码.
     */
    public static final int ERROR_CODE = -1;
    /**
     * 错误信息.
     */
    public static final String ERROR_MESSAGE = "抱歉，系统错误，我们马上报告老板！";
    /**
     * 序列化标识
     */
    private static final long serialVersionUID = 4893280118017319089L;

    private int code;

    private String msg;

    private T data;

    /**
     * Instantiates a new wrapper. default code=200
     */
    public Result() {
        this(SUCCESS_CODE, SUCCESS_MESSAGE);
    }

    /**
     * Instantiates a new wrapper.
     *
     * @param code    the code
     * @param message the message
     */
    public Result(int code, String message) {
        this(code, message, null);
    }

    /**
     * Instantiates a new wrapper.
     *
     * @param code    the code
     * @param message the message
     * @param result  the result
     */
    public Result(int code, String message, T result) {
        super();
        this.setCode(code);
        this.setMsg(message);
        this.setData(result);
    }

    /**
     * 判断是否成功： 依据 Wrapper.SUCCESS_CODE == this.code
     *
     * @return code=200,true;否则 false.
     */

    @JsonIgnore(true)
    public boolean isSuccess() {
        return Result.SUCCESS_CODE == this.code;
    }

}
